package com.gopay.solution.service.solu.impl;

import java.io.Serializable;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.constants.txncd.IntTxnCd;
import com.gopay.common.core.dic.manager.CpsTransInfoQueryManager;
import com.gopay.common.core.dic.manager.DicPayChannelManager;
import com.gopay.common.domain.cps.CpsTransInfo;
import com.gopay.common.domain.dic.DicPayChannel;
import com.gopay.common.domain.solution.ProfitReq;
import com.gopay.common.domain.solution.SolutionRequest;
import com.gopay.common.domain.solution.SolutionResponse;
import com.gopay.common.exception.validator.GopayCheckException;
import com.gopay.common.constants.solution.SolutionTypeConstant;

/**
 * 交易返润方案服务
 * 
 * @ssTransProfitService.java
 * @author wanggang
 * @2014-7-11 上午11:16:09  www.gopay.com.cn Inc.All rights reserved.
 */
@Service(value = "ssTransProfitService")
public class SsTransProfitServiceImpl extends SolutionServiceImpl {

    @Resource(name = "dicPayChannelManager")
    private DicPayChannelManager dicPayChannelManager;
    @Resource(name = "cpsTransInfoQueryManager")
    private CpsTransInfoQueryManager cpsTransInfoQueryManager;    
    
    @Override
    protected String getSolutionType() {
        return SolutionTypeConstant.TRANS_PROFIT;
    }
    
    public SolutionRequest beforeProcess(SolutionRequest request) throws Exception{

        

        Serializable input = request.getRequest();
        if (null == input || !(input instanceof ProfitReq)) {
            return null;
        }
        
        ProfitReq req = (ProfitReq) input;
        String profitMonth = req.getProfitMonth();
        String payChann = req.getPayChannel();
        IntTxnCd intTxnCd = req.getIntTxnCd();
        String acctType = req.getAcctType();
        BigDecimal amount = req.getAmount();

        if (!StringUtils.equals("02", acctType) && !StringUtils.equals("01", acctType)) {
            throw new GopayCheckException(ProcCodeConstants.PROC_CODE_100E1027);
        }
        
        if(null == amount || amount.compareTo(new BigDecimal("0.00")) < 0){
            throw new GopayCheckException(ProcCodeConstants.PROC_CODE_100E1041);
        }
        
        if(StringUtils.isEmpty(payChann)){
            throw new GopayCheckException(ProcCodeConstants.PROC_CODE_100E5006);
        }
        
        if(null == intTxnCd){
            throw new GopayCheckException(ProcCodeConstants.PROC_CODE_100E7006);
        }
        
        DicPayChannel p = dicPayChannelManager.get(payChann);
        if (null == p) {
            throw new GopayCheckException(ProcCodeConstants.PROC_CODE_100E5006);
        }

        CpsTransInfo info = cpsTransInfoQueryManager.get(intTxnCd.value);
        if (null == info) {
            throw new GopayCheckException(ProcCodeConstants.PROC_CODE_100E7006);
        }
        
        if(StringUtils.isBlank(profitMonth)){
            throw new GopayCheckException(ProcCodeConstants.PROC_CODE_100E1047);
        }else{
            SimpleDateFormat df = new SimpleDateFormat("yyyyMM");   
            try{
                df.parse(profitMonth);
            } catch (ParseException e) {
                throw new GopayCheckException(ProcCodeConstants.PROC_CODE_100E1047);
            }
        }

        return request;
    }

    public SolutionResponse afterProcess(SolutionResponse response) {

        

        return response;
    }
}
